 #include "indirect_list.hpp"

template<class T>
indirect_list<T>::indirect_list()
  :size_(0)
  ,max_size_(10)
  ,table_(new T*[max_size_])
{
}
 template<class T>
indirect_list<T>:: ~indirect_list()
{
  for(size_t i = 0; i < size_; ++i)
    {
      delete table_[i];
    }
   delete [] table_;
}
  template<class T>
  const size_t& indirect_list<T>::size()
{
  return size_;
}
 template<class T>
bool indirect_list<T>::find(size_t pos, T& t) const
{
  if (pos >= size_)
    {
      return false;
    }
   t = *table_[pos];
   return true;
}
template<class T>
bool indirect_list<T>::remove(size_t pos, T& t)
{
  if (find(pos, t))
    {
      --size_;
      for (size_t i = pos; i < size_; ++i)
	{
	  table_[i] = table_[i + 1];
	}
      return true;
    }
  return false;
}
template<class T>
bool indirect_list<T>::insert(const T& t)
{
  if (size_ > max_size_)
    {
      return false;
    }
  table_[size_] = new T();
  *table_[size_] = t;
  ++size_;
  return true;
}




TEST(il, insert)
{
  indirect_list<int> il;
  ASSERT_TRUE(il.insert(3));
  int k = 0;
  size_t size = 1;
  EXPECT_TRUE(il.find(0, k));
  EXPECT_EQ(3, k);
  EXPECT_EQ(size, il.size());

  ASSERT_TRUE(il.insert(6));
  ASSERT_TRUE(il.insert(4));
  ASSERT_TRUE(il.insert(7));
  size = 4;
  EXPECT_EQ(size, il.size());
}
TEST(il, remove)
{
  indirect_list<int> il;
  ASSERT_TRUE(il.insert(1));
  ASSERT_TRUE(il.insert(2));
  ASSERT_TRUE(il.insert(3));
  ASSERT_TRUE(il.insert(4));
  ASSERT_TRUE(il.insert(5));
  size_t size = 5;
  ASSERT_EQ(size, il.size());
  int k = 0;
  size_t pos = 0;
  ASSERT_TRUE(il.remove(pos, k));
  size = 4;
  ASSERT_EQ(size, il.size());
  il.find(0, k);
  ASSERT_EQ(2, k);

  il.find(1, k);
  ASSERT_EQ(3, k);

  il.find(2, k);
  ASSERT_EQ(4, k);

  il.find(3, k);
  ASSERT_EQ(5, k);
}
